<script setup>

/**
 * ,
	{
		"path": "/Recruit/",
		"name": "Recruit",
		"vName": "招聘管理",
		"children": [
			{
				"path": "/Recruit/recruitSchedule",
				"name": "recruitSchedule",
				"vName": "招聘进度"
			},
			{
				"path": "/Recruit/talentPool",
				"name": "talentPool",
				"vName": "人才库"
			}
		]
	}


    
  recruitSchedule: () => import("../views/Recruit/RecruitSchedule.vue"),
  talentPool: () => import("../views/Recruit/TalentPool.vue"),
 */


import axios from 'axios';
import { onMounted, ref, reactive } from 'vue';
import { ElMessage } from 'element-plus';

const viewData = ref([]);
const para = ref({
    time: [],
    beginTime: null,
    endTime: null
});

const obj = ref({
    talent_name: null,
    sex: null,
    postId: null,
    postName: null,
    workKind: null,
    education: null,
    certification: null,
    workAge: null,
    phone: null,
    idCard: null,
    address: null,
    createTime: null,
    state: null
});

const isUpdate = ref(false);
const showDialog = ref(false);
const pageInfo = reactive({
    pageNum: 1,
    pageSize: 5,
    total: 0
});

let getList = () => {
    axios({
        method: "POST",
        url: `/api/talentPool/list`,
        data: {
            beginTime: (para.value.time && para.value.time.length > 0)? para.value.time[0] : null,
            endTime: (para.value.time && para.value.time.length > 1)? para.value.time[1] : null,
            ...pageInfo
        }
    }).then((resp) => {
        if (resp.data.code === 200) {
            viewData.value = resp.data.data.list;
            viewData.value.forEach(e => {
                switch (e.state) {
                    case 0:
                        e.state = '待招聘';
                        break;
                    case 1:
                        e.state = '招聘中';
                        break;
                    case 2:
                        e.state = '笔试考核中';
                        break;
                    case 3:
                        e.state = '面试考核中';
                        break;
                    case 4:
                        e.state = '招聘成功';
                        break;
                    case 5:
                        e.state = '招聘失败';
                        break;
                    default:
                        e.state = '招聘已取消';
                }
            });
            pageInfo.total = resp.data.data.count ? resp.data.data.count : 0;
        } else {
            ElMessage.error(resp.data.msg);
            console.log("出现逻辑异常:", resp);
        }
    }).catch((e) => {
        console.log("出现异常:", e);
    })
};

const selectobj = () => {
    getList();
};

const addobj = () => {
    obj.value.talent_name = null;
    obj.value.sex = null;
    obj.value.postId = null;
    obj.value.workKind = null;
    obj.value.education = null;
    obj.value.certification = null;
    obj.value.workAge = null;
    obj.value.phone = null;
    obj.value.idCard = null;
    obj.value.address = null;
    obj.value.createTime = null;

    isUpdate.value = false;
    showDialog.value = true;
};

const handleUpdate = (row) => {
    obj.value.talent_name = row.talent_name;
    obj.value.sex = row.sex;
    obj.value.postId = row.postId;
    obj.value.workKind = row.workKind;
    obj.value.education = row.education;
    obj.value.certification = row.certification;
    obj.value.workAge = row.workAge;
    obj.value.phone = row.phone;
    obj.value.idCard = row.idCard;
    obj.value.address = row.address;
    obj.value.id = row.id;

    showDialog.value = true;
    isUpdate.value = true;
};

const handleDelete = (row) => {

    axiosPara.method = "DELETE";
    axiosPara.url = "/api/talentPool/delete" + row.id;

    axios(axiosPara).then((resp) => {
        if (resp.data.code === 200) {
            getList();
        } else {
            ElMessage.error(resp.data.msg);
            console.log("出现逻辑异常:", resp);
        }
    }).catch((e) => {
        console.log("出现异常:", e);
    });
};

const handleRecruit = (row) => {
    obj.value.state = 1;
    obj.value.id = row.id;
    const axiosPara = {
        data: obj.value
    };

    axiosPara.method = "PATCH";
    axiosPara.url = "/api/talentPool/recruit";

    axios(axiosPara).then((resp) => {
        if (resp.data.code === 200) {
            getList();
        } else {
            ElMessage.error(resp.data.msg);
            console.log("出现逻辑异常:", resp);
        }
    }).catch((e) => {
        console.log("出现异常:", e);
    });
};

const submitData = () => {
    const axiosPara = {
        data: obj.value
    };

    if (isUpdate.value) {
        axiosPara.method = "PATCH";
        axiosPara.url = "/api/talentPool/update";
    } else {
        axiosPara.method = "POST";
        axiosPara.url = "/api/talentPool/add";
    }

    axios(axiosPara).then((resp) => {
        if (resp.data.code === 200) {
            showDialog.value = false;
            getList();
        } else {
            ElMessage.error(resp.data.msg);
            console.log("出现逻辑异常:", resp);
        }
    }).catch((e) => {
        console.log("出现异常:", e);
    });

};

onMounted(() => {
    getList();
});
</script>

<template>
    <div>
        <header class="header">
            <div class="wrapper">
                <div class="s-block">
                    <span class="s-demonstration">时间</span>
                    <el-date-picker v-model="para.time" value-format="YYYY-MM-DD HH:mm:ss" type="datetimerange"
                        placeholder="Select date and time" size="small" />
                </div>
            </div>
            <div class="w-button">
                <button @click="selectobj">查询</button>
                <button @click="addobj">新增</button>
            </div>
        </header>

        <main>
            <el-table :data="viewData">
                <el-table-column prop="talentName" label="姓名"></el-table-column>
                <el-table-column prop="sex" label="性别"></el-table-column>
                <el-table-column prop="postName" label="应聘职务"></el-table-column>
                <el-table-column prop="workKind" label="应聘类型"></el-table-column>
                <el-table-column prop="education" label="学历"></el-table-column>
                <el-table-column prop="certification" label="证书"></el-table-column>
                <el-table-column prop="workAge" label="工作年龄"></el-table-column>
                <el-table-column prop="phone" label="手机号"></el-table-column>
                <el-table-column prop="idCard" label="身份证号"></el-table-column>
                <el-table-column prop="address" label="地址"></el-table-column>
                <el-table-column prop="createTime" label="创建时间"></el-table-column>
                <el-table-column prop="state" label="状态"></el-table-column>
                <el-table-column label="审批">
                    <template #default="scope">
                        <el-button size="small" @click="handleUpdate(scope.row)">修改</el-button>
                        <el-button size="small" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                        <el-button size="small" type="danger" @click="handleRecruit(scope.row)">招聘</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </main>

        <footer>
            <el-pagination v-model:current-page="pageInfo.pageNum" v-model:page-size="pageInfo.pageSize"
                :page-sizes="[5, 10, 20, 50]" layout="total, sizes, prev, pager, next, jumper" :total="pageInfo.total"
                @size-change="getList" @current-change="getList" />

        </footer>

        <el-dialog v-model="showDialog">
            <div>
                <span>姓名:</span>
                <el-input v-model="obj.name"></el-input>
            </div>
            <div>
                <span>性别:</span>
                <el-input v-model="obj.sex"></el-input>
            </div>
            <div>
                <span>应聘职务:</span>
                <el-input v-model="obj.postId"></el-input>
            </div>
            <div>
                <span>工作类型:</span>
                <el-input v-model="obj.workKind"></el-input>
            </div>
            <div>
                <span>学历:</span>
                <el-input v-model="obj.education"></el-input>
            </div>
            <div>
                <span>证书:</span>
                <el-input v-model="obj.certification"></el-input>
            </div>
            <div>
                <span>工作年龄:</span>
                <el-input v-model="obj.workAge"></el-input>
            </div>
            <div>
                <span>手机号:</span>
                <el-input v-model="obj.phone"></el-input>
            </div>
            <div>
                <span>身份证号:</span>
                <el-input v-model="obj.idCard"></el-input>
            </div>
            <div>
                <span>地址:</span>
                <el-input v-model="obj.address"></el-input>
            </div>
            <button @click="submitData">提交</button>
        </el-dialog>

    </div>
</template>

<style scoped>
.header {
    display: flex;
    flex-direction: column;
}

.wrapper {
    display: flex;
    flex-direction: row;
}

.wrapper>div {
    display: flex;
    flex-direction: row;
    margin-right: 1vw;
    gap: 0.5vw;
}

.wrapper>div>span {
    flex: 0 0 auto;
    /* flex-basis: auto; */
    /* flex-shrink: 0; */
}

.w-button {
    display: flex;
    flex-direction: row;
}

.w-button>button {
    margin-right: 1vw;
}
</style>